package boddaert.philippe.cashGame.parser.game;

import au.id.jericho.lib.html.Element;
import boddaert.philippe.cashGame.Game;
import boddaert.philippe.cashGame.GameConfiguration;
import boddaert.philippe.cashGame.site.WebGame;

public abstract class WebGameParser implements IWebGameParser {

	public Game parse(WebGame game) {
		if (game == null)
			throw new IllegalArgumentException();

		Element gameInHtmlFormat = game.getGameInHtmlFormat();

		Game parsedGame = new Game();
		parsedGame.setTitle(parseTitle(gameInHtmlFormat));
		parsedGame.addConfiguration(getNewGameConfiguration());
		parsedGame.getConfiguration().setUrl(parseUrl(gameInHtmlFormat));
		parsedGame.getConfiguration().setBestPrice(
				parseBestPrice(gameInHtmlFormat));
		parsedGame.getConfiguration().setNumberNewAvailable(
				parseNewAvailable(gameInHtmlFormat));
		parsedGame.getConfiguration().setNumberSecondHandAvailable(
				parseSecondHandAvailable(gameInHtmlFormat));

		return parsedGame;
	}

	/**
	 * parse the title game
	 * 
	 * @param game
	 *            : a HTML representation of a game
	 * @return the title game, null if parsing failed.
	 */
	protected abstract String parseTitle(Element game);

	/**
	 * parse the url game
	 * 
	 * @param game
	 *            : a HTML representation of a game
	 * @return the url game, null if parsing failed.
	 */
	protected abstract String parseUrl(Element game);

	/**
	 * parse the best price of a game
	 * 
	 * @param game
	 *            : a HTML representation of a game
	 * @return the best price game, -1 if parsing failed.
	 */
	protected abstract double parseBestPrice(Element game);

	/**
	 * parse the number of new available game
	 * 
	 * @param game
	 *            : a HTML representation of a game
	 * @return the number of new available game, -1 if parsing failed.
	 */
	protected abstract int parseNewAvailable(Element game);

	/**
	 * parse the number of second hand available game
	 * 
	 * @param game
	 *            : a HTML representation of a game
	 * @return the number of second hand available game, -1 if parsing failed.
	 */
	protected abstract int parseSecondHandAvailable(Element game);

	/**
	 * expose a new Game configuration
	 * @return a GameConfiguration
	 */
	protected abstract GameConfiguration getNewGameConfiguration();

}
